package com.lg.okframe.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.facebook.drawee.view.SimpleDraweeView;
import com.lg.okframe.R;
import com.lg.okframe.activity.BookReadActivity;
import com.lg.okframe.entities.HomeData;
import com.lg.okframe.fragment.HomeFragment;
import com.lg.okframe.fragment.OneFragment;
import com.lg.okframe.fragment.ThreeFragment;
import com.lg.okframe.fragment.TwoFragment;
import com.lg.okframe.view.GlideRoundTransform;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by LG on 2018/7/31.
 */
public class HomeAdapter extends RecyclerView.Adapter implements OnBannerListener {
    private static final int HEAD = 1;
    private static final int CONTENTQ = 2;
    List<HomeData.DataBean.GuanggaoBean> guanggao;
    Context context;
    private ArrayList<String> list_path;
    private ArrayList<String> list_title;
    HomeFragment fragment;

    public HomeAdapter(List<HomeData.DataBean.GuanggaoBean> guanggao, Context context, HomeFragment fragment) {
        this.context = context;
        this.guanggao = guanggao;
        this.fragment = fragment;

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate;
        if (HEAD == viewType) {
            inflate = View.inflate(context, R.layout.home_adapter_head, null);
            return new HeadHolder(inflate);
        } else {
            inflate = View.inflate(context, R.layout.home_adapter_item, null);
            return new Holder(inflate);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return HEAD;
        } else {
            return CONTENTQ;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        int itemViewType = getItemViewType(position);

        if (itemViewType == CONTENTQ) {
            Holder holder1 = (Holder) holder;
            holder1.item_listen.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    fragment.ToStartActivity(BookReadActivity.class);
                }
            });

/*            holder1.name.setText(guanggao.get(position).getName());
            holder1.add_time.setText(guanggao.get(position).getAddtime());
            Uri uri = Uri.parse("http://www.jrs2018.cn"+guanggao.get(position).getLogo());
            holder1.img.setImageURI(uri);*/
//            Glide.with(context).load("http://www.jrs2018.cn" + guanggao.get(position).getLogo()).into(holder1.img);
            Glide.with(context).load("http://www.jrs2018.cn" + guanggao.get(position).getLogo()).transform(new GlideRoundTransform(context, 30))
                    .into(holder1.img);
        } else {

            HeadHolder holder2 = (HeadHolder) holder;
            initBanner(holder2.banner);
            ArrayList fragmentList = new ArrayList<>();
            ArrayList list_Title = new ArrayList<>();
            fragmentList.add(new OneFragment());
            fragmentList.add(new TwoFragment());
            fragmentList.add(new ThreeFragment());
            list_Title.add("技术博文");
            list_Title.add("面试宝典");
            list_Title.add("前沿资讯");
            holder2.viewpager.setAdapter(new MyPagerAdapter(fragment.getFragmentManager(), context, fragmentList, list_Title));
            holder2.tablayout.setupWithViewPager(holder2.viewpager);
        }



    }

    private void initBanner(Banner banner) {
        //放图片地址的集合
        list_path = new ArrayList<>();
        //放标题的集合
        list_title = new ArrayList<>();

        list_path.add("http://ww4.sinaimg.cn/large/006uZZy8jw1faic21363tj30ci08ct96.jpg");
        list_path.add("http://ww4.sinaimg.cn/large/006uZZy8jw1faic259ohaj30ci08c74r.jpg");
        list_path.add("http://ww4.sinaimg.cn/large/006uZZy8jw1faic2b16zuj30ci08cwf4.jpg");
        list_path.add("http://ww4.sinaimg.cn/large/006uZZy8jw1faic2e7vsaj30ci08cglz.jpg");
        list_title.add("屏幕适配最新方案来袭");
        list_title.add("google发布安卓8.0系统");
        list_title.add("张小龙推出微信小程序");
        list_title.add("腾讯又出新版本产品");

        //设置内置样式，共有六种可以点入方法内逐一体验使用。
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE);
        //设置图片加载器，图片加载器在下方
        banner.setImageLoader(new MyLoader());
        //设置图片网址或地址的集合
        banner.setImages(list_path);
        //设置轮播的动画效果，内含多种特效，可点入方法内查找后内逐一体验
        banner.setBannerAnimation(Transformer.Accordion);
        //设置轮播图的标题集合
        banner.setBannerTitles(list_title);
        //设置轮播间隔时间
        banner.setDelayTime(3000);
        //设置是否为自动轮播，默认是“是”。
        banner.isAutoPlay(true);
        //设置指示器的位置，小点点，左中右。
        banner.setIndicatorGravity(BannerConfig.CENTER)
                //以上内容都可写成链式布局，这是轮播图的监听。比较重要。方法在下面。
                .setOnBannerListener(this)
                //必须最后调用的方法，启动轮播图。
                .start();

    }

    @Override
    public void OnBannerClick(int position) {

    }

    //自定义的图片加载器
    public class MyLoader extends ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            Glide.with(context).load((String) path).into(imageView);
        }
    }


    @Override
    public int getItemCount() {
        return guanggao.size();
    }

    class Holder extends RecyclerView.ViewHolder {

        private final SimpleDraweeView img;
        private final TextView name;
        private final TextView add_time;
        private final LinearLayout item_listen;

        public Holder(View itemView) {
            super(itemView);
            img = (SimpleDraweeView) itemView.findViewById(R.id.img);
            name = (TextView) itemView.findViewById(R.id.name);
            add_time = (TextView) itemView.findViewById(R.id.add_time);
            item_listen = (LinearLayout) itemView.findViewById(R.id.item_listen);

        }
    }

    class HeadHolder extends RecyclerView.ViewHolder {

        private final Banner banner;
        private final TabLayout tablayout;
        private final ViewPager viewpager;

        public HeadHolder(View itemView) {
            super(itemView);
            banner = (Banner) itemView.findViewById(R.id.banner);
            tablayout = (TabLayout) itemView.findViewById(R.id.tablayout);
            viewpager = (ViewPager) itemView.findViewById(R.id.viewpager);


        }
    }
}
